#!/usr/bin/perl

sub rtrim($);

open(MYINPUTFILE, "<$ARGV[0]");
my($state) = 0; # normal mode, 1 is template mode 
while(<MYINPUTFILE>)
 {
     my($line) = $_;

     chomp($line);

     if ($line =~ /^\/\*\|\[/) 
     {
	 $state = 1;
     } 
     elsif ($line =~ /^\]\|\*\//) 
     {
	 $state = 0;
     }
     else
     {
	 if ($state == 0) 
	 {
	     print "$line\n";
	 }
	 elsif ($state == 1) 
	 {
	     $line =~ s/"/\\"/g;
	     $line =~ s/\\n/\\\\n/g;
	   
	     if ($line =~ /.+\s+\*\[(.+)\]\s*$/)
	     {
		 @line = split(/\*\[/, $line);
		 $tmp = rtrim(@line[0]);
		 print " sprintf \"$tmp\\n\" ($1) +\n";
	     }
	     else 
	     {
		 print "         \"$line\\n\" +\n";
	     }
	 }
     }
 }

# Right trim function to remove trailing whitespace
sub rtrim($)
{
    my $string = shift;
    $string =~ s/\s+$//;
    return $string;
}
